package com.study.web.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.study.common.exception.BizException;
import com.study.common.page.PageBean;
import com.study.common.utils.ResponseUtil;
import com.study.web.context.SpringMVCActionSupport;
import com.study.web.entity.SysParams;
import com.study.web.entity.SysUser;
import com.study.web.service.sysparams.SysParamsService;

/**
 * 
 * All rights Reserved, Designed By yaojinshan
 * @Title:  SysParamsController.java   
 * @Package com.study.web.action   
 * @Description:    系统参数管理控制器
 * @author: yaojinshan     
 * @date:   2018年6月27日 下午3:37:20   
 * @version V1.0 
 * @Copyright: 2018 All users All rights reserved. 
 * 注意：注意：本内容仅限于学习springmvc + spring + maven + mybatis学习使用
 */
@SuppressWarnings("all")
@RequestMapping(value = "/admin/sysparams")
@Controller
public class SysParamsController extends SpringMVCActionSupport {
	Logger logger = LoggerFactory.getLogger(SysParamsController.class);
	@Resource
    private RedisTemplate redisTemplate;
	
	@Resource
	private SysParamsService sysParamsService;

	/**
	 * @Description 获取列表页面数据 <pre>
	 * @author 作者: yaojinshan <pre>
	 * @date 2018年7月2日 上午10:45:41 <pre> 
	 */
	@RequestMapping(value = "/queryListSysParamsData")
	@ResponseBody
	@RequiresPermissions("sysParamsList:view")
	public String queryListSysParamsData(){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String msg = "获取系统参数列表数据失败";
		String result = ResponseUtil.RES_FAIL;
		resultMap.put("msg", msg);
		String keyName = getString("keyName");
		String startCreateTime = getString("startCreateTime");
		String endCreateTime = getString("endCreateTime");
		String startUpdateTime = getString("startUpdateTime");
		String endUpdateTime = getString("endUpdateTime");
		String opUser = getString("opUser");
		String isUse = getString("isUse");
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("keyName", keyName);
		paramMap.put("opUser", opUser);
		paramMap.put("isUse", isUse);
		try {
			paramMap.put("startCreateTime", (startCreateTime == null || startCreateTime == "") ? null : DateUtils.parseDate(startCreateTime, "yyyy-MM-dd HH:mm:ss"));
			paramMap.put("endCreateTime",(endCreateTime == null || endCreateTime == "") ? null : DateUtils.parseDate(endCreateTime, "yyyy-MM-dd HH:mm:ss"));
			paramMap.put("startUpdateTime", (startUpdateTime == null || startUpdateTime == "") ? null : DateUtils.parseDate(startUpdateTime, "yyyy-MM-dd HH:mm:ss"));
			paramMap.put("endUpdateTime",(endUpdateTime == null || endUpdateTime == "") ? null : DateUtils.parseDate(endUpdateTime, "yyyy-MM-dd HH:mm:ss"));
			PageBean pageBean = sysParamsService.getSysParamsListData(getPageParam(), paramMap);
			if (pageBean != null && pageBean.getRecordList() != null && pageBean.getRecordList().size() > 0) {
				resultMap.put("rows", pageBean.getRecordList());
				resultMap.put("total", pageBean.getTotalCount());
			} else {
				resultMap.put("rows", new ArrayList<Object>());
				resultMap.put("total", 0);
			}
			result = ResponseUtil.RES_SUCCESS;
			msg = "获取系统参数列表数据成功";
		}catch (BizException e) {
			e.printStackTrace();
			msg = e.getMessage();
		} catch (ParseException e) {
			e.printStackTrace();
			msg = "请求参数格式不不正确！";
		}finally {
		    resultMap.put("result", result);
            resultMap.put("msg", msg);
            return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 保存添加页数据
	 * @return
	 */
	@RequestMapping("/addSysParams")
	@ResponseBody
	@RequiresPermissions("sysParamsAdd:view")
	public String addSysParams() {
		String result = ResponseUtil.RES_FAIL;
		String msg = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		
		String keyName = getString("keyName");
		String keyEname = getString("keyEname");
		String keyValue = getString("keyValue");
		String status = getString("status");
		String remark = getString("remark");
		
		SysParams sysParams = new SysParams();
		if(!StringUtils.isEmpty(keyName)){
			sysParams.setKeyName(keyName);
		}
		if(!StringUtils.isEmpty(keyEname)){
			sysParams.setKeyEname(keyEname);
		}
		if(!StringUtils.isEmpty(keyValue)){
			sysParams.setKeyValue(keyValue);
		}
		if(!StringUtils.isEmpty(status)){
			sysParams.setStatus(Integer.parseInt(status));
		}
		if(!StringUtils.isEmpty(remark)){
			sysParams.setRemark(remark);
		}
		SysUser user= (SysUser) SecurityUtils.getSubject().getPrincipal();
		if(user != null){
			sysParams.setOpUser(user.getLoginName());
		}
		sysParams.setCreateTime(new Date());
		sysParams.setIsDelete(1);
		try {
			sysParamsService.saveSysParamsAddPageData(sysParams);
			result = ResponseUtil.RES_SUCCESS;
			msg = "新增系统参数成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "系统或网络错误！";
		} finally {
			retMap.put("result", result);
			retMap.put("msg", msg);
			return JSON.toJSONString(retMap);
		}
	}
	
	/**
	 * 通过Id获取系统参数记录
	 * @return
	 */
	@RequestMapping(value = "/getSysParamsById")
	@ResponseBody
	public String getSysParamsById(Long sysParamsId) {
		String result = ResponseUtil.RES_FAIL;
		String msg = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			SysParams sysParams = sysParamsService.getById(sysParamsId);
			if(sysParams == null){
				msg = "请求系统参数对象不存在！";
			}else{
				retMap.put("sysParams", sysParams);
				result = ResponseUtil.RES_SUCCESS;
				msg = "查询系统参数成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			retMap.put("result", result);
			retMap.put("msg", msg);
			return JSON.toJSONString(retMap);
		}
	}
	
	/**
	 * 保存修改页数据
	 * @return
	 */
	@RequestMapping("/updateSysParams")
	@ResponseBody
	@RequiresPermissions("sysParamsUpdate:view")
	public String updateSysParams() {
		String result = ResponseUtil.RES_FAIL;
		String msg = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		String id = getString("sysParamsId");
		String keyName = getString("keyName");
		String keyEname = getString("keyEname");
		String keyValue = getString("keyValue");
		String status = getString("status");
		String remark = getString("remark");
		
		SysParams sysParams = new SysParams();
		if(!StringUtils.isEmpty(keyName)){
			sysParams.setKeyName(keyName);
		}
		if(!StringUtils.isEmpty(keyEname)){
			sysParams.setKeyEname(keyEname);
		}
		if(!StringUtils.isEmpty(keyValue)){
			sysParams.setKeyValue(keyValue);
		}
		if(!StringUtils.isEmpty(status)){
			sysParams.setStatus(Integer.parseInt(status));
		}
		if(!StringUtils.isEmpty(remark)){
			sysParams.setRemark(remark);
		}
		
		try {
			if(StringUtils.isEmpty(id)){
				throw new Exception();
			}
			sysParams.setId(Long.parseLong(id));
			sysParamsService.saveSysParamsUpdatePageData(sysParams);
			result = ResponseUtil.RES_SUCCESS;
			msg = "修改系统参数成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "系统或网络错误！";
		} finally {
			retMap.put("result", result);
			retMap.put("msg", msg);
			return JSON.toJSONString(retMap);
		}
	}
	/**
	 * 删除数据
	 * @return
	 */
	@RequestMapping("/batchdeletedata")
	@ResponseBody
	@RequiresPermissions("sysParamsDelete:action")
	public String batchDeleteData() {
		String result = ResponseUtil.RES_FAIL;
		String ids = getString("ids");
		String[] idArr = ids.split(",");
		List<SysParams> sysParamsList = new ArrayList<SysParams>();
		for(int i = 0; i < idArr.length; i++){
			SysParams sysParams = new SysParams();
			sysParams.setId(Long.parseLong(idArr[i]));;
			sysParamsList.add(sysParams);
		}
		String msg = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			sysParamsService.batchDeleteSysParamsData(sysParamsList);
			result = ResponseUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			msg = "系统或网络错误！";
		} finally {
			retMap.put("result", result);
			retMap.put("msg", msg);
			return JSON.toJSONString(retMap);
		}
	}
}
